package com.inossem.warehousestorage_inossem.activity.allocation.out_storage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.extra.AllocationOutStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationChooseInOrderAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutStorageBaseInfoBean;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.util.ArrayList;
import java.util.List;

/**
 * 生产入库单
 *
 * @author 詹建宇
 * @time on 2018/3/29 11:53
 * @email jianyu.zhan@inossem.com
 */
public class AllocationOutChooseInOrderActivity extends BaseActivity implements BaseInteface {

    private TextView title;
    private PullableListView listView;
    private String mCondition = "";

    private List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean> inputOrderList = new ArrayList<>();//生产入库单列表
    private AllocationChooseInOrderAdapter adapter;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_allocating_listview);

        initView(this);
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) findViewById(R.id.headerTitle);
        listView = (PullableListView) activity.findViewById(R.id.listView);

        listView.setPullDownFlag(false);
        listView.setPullUpFlag(false);
    }

    @Override
    public void initData(Activity activity) {
        title.setText("生产入库单");
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        inputOrderList = (List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean>) getIntent().getSerializableExtra(AllocationOutStorageExtra.ALLOCATION_INORDER_LIST);
        addListData(activity, inputOrderList);
    }

    private void addListData(Activity activity, List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean> list) {
        adapter = new AllocationChooseInOrderAdapter(this, mCondition, list, new SearchListener() {
            @Override
            public void onNoDoubleClickListener(String condition) {
                String checkResult = check(condition);
                if (checkResult == null) {
                    mCondition = condition;
                    getData(mCondition);
                } else {
                    Utils.middleToast(activity, checkResult);
                }
            }
        });
        listView.setAdapter(adapter);
    }

    private String check(String condition) {
        String result = null;
        if (TextUtils.isEmpty(condition)) {
            result = getResources().getString(R.string.activity_refund_storage_picking_list_the_query_condition_must_not_be_empty);
        }
        return result;
    }

    private void getData(String condition) {
        List<AllocationOutStorageBaseInfoBean.BaseInfoBodyBean.InputOrderListBean> list = AllocationUtils.filterInputOrderList(condition, inputOrderList);
        addListData(this, list);
    }

    public interface SearchListener {
        void onNoDoubleClickListener(String condition);
    }

}
